/**
* @author   Frederic Saunier - www.tekool.net
* @since    2008/08/10
*
* Copyright (C) 2006 Frederic Saunier
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

package net.tekool.projects.objscomp;

import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

import net.tekool.projects.objscomp.business.ClassDescriptor;
import net.tekool.projects.objscomp.commands.ConcatenateCode;
import net.tekool.projects.objscomp.commands.CreateDebugHTMLFile;
import net.tekool.projects.objscomp.commands.ListClasses;
import net.tekool.projects.objscomp.commands.OptimizeCode;
import net.tekool.projects.objscomp.commands.WriteCode;

/**
 * Main class
 */
public class Main
{
	private static final boolean DEBUG = true;
	private static final Logger log = Logger.getLogger(Main.class.getName());
	
	private String src;
	private String out;
	private String html;
	private ArrayList<ClassDescriptor> classes;

	private Main()
	{
		super();
	}

	public static final void main(String[] args)
	{
		if(DEBUG)
			log.entering(Main.class.getName(), "main", args);
		
		Main jscomp = new Main();

		int result = 1;
		try
		{
			jscomp.setup();
			jscomp.start(args);
			result = 0;
		}
		catch (Exception x)
		{
			System.err.println(x.toString());
			System.err.println(x.getStackTrace().toString());
		}
		finally
		{
			jscomp.teardown();
		}
		
		if(DEBUG)
			log.exiting(Main.class.getName(), "main", Integer.valueOf(result));
		
		System.exit(result);
	}
	
	private void start(String[] args)
    {
		for(int i=0; i<args.length; i++)
		{
			String arg = args[i];
			
			if( arg.substring(0,3).equals("src") )
				src = arg.substring(4);
			else
			if( arg.substring(0,3).equals("out") )
				out = arg.substring(4);
			else
			if( arg.substring(0,4).equals("html") )
				html = arg.substring(5);
		}

		if(src == "")
			throw(new Error("Missing required 'src' argument"));
		
		if(html == "" && out == "")
			throw(new Error("An 'out' or 'html' argument need to be specified"));

		listClasses();

		if(out != "")
			writeCode();
		
		if(html != "")
			writeHTML();
    }

	private void listClasses()
    {
		classes = new ArrayList<ClassDescriptor>();
		ListClasses listClassesCmd = new ListClasses( src, classes );
		
		long t = new Date().getTime();
		listClassesCmd.execute();
		System.out.println("listClassesCmd.execute() -> " + (new Date().getTime()-t));	    
    }

	private void writeHTML()
    {
		StringBuilder tags = new StringBuilder();
		
		long t = new Date().getTime();

		CreateDebugHTMLFile createScriptFiles = new CreateDebugHTMLFile
		(
			classes,
			html
		);
		createScriptFiles.execute();

		System.out.println(tags);
		System.out.println("createScriptFiles.execute() -> " + (new Date().getTime()-t));
    }

	private void writeCode()
    {
		StringBuilder code = new StringBuilder();
		
		long t = new Date().getTime();

		ConcatenateCode concatenate = new ConcatenateCode( classes, code );
		concatenate.execute();
		
		System.out.println("ConcatenateCode.execute() -> " + (new Date().getTime()-t));
	
		OptimizeCode optimize = new OptimizeCode( code );
		optimize.execute();
		
		WriteCode write = new WriteCode( code.toString(), out );
		write.execute();
		
		//System.out.println(code);
    }

	public void setup()
    {
	    
    }
	
	public void teardown()
	{
		if(DEBUG)
			log.exiting(Main.class.getName(), "teardown");		
	}
}